home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_03 / letters / allocdma.c < prev   
Text File  |  1994-01-18  |  2KB  |  69 lines

  1. @CSOURCE8 = #define BYTE char<R>
  2. typedef unsigned int WORD;<R>
  3. typedef unsigned long BIG;<R>
  4. <R>
  5. #define JUNKCHUNK (128)<R>
  6. /* how we will search for physical<R>
  7. boundaries. (Must be >>= sizeof(JUNK)) */<R>
  8. <R>
  9. /* produce a big physical address. This<R>
  10. is "large" mode code; " FP_SEG" etc. are<R>
  11. Turbo C macros which extract the two<R>
  12. parts of an 80x86-style segment-offset<R>
  13. pointer. */<R>
  14. <R>
  15. #define PHYSICAL(x) ((BIG)((((BIG)<R>
  16.     (FP_SEG(x)))<<<<4)+((BIG)FP_OFF(x))))<R>
  17.      <R>
  18. typedef struct JK<R>
  19.   struct JK *next;<R>
  20. } JUNK;<R>
  21. <R>
  22. _f void *mustallocdma(WORD size) {<R>
  23.   BYTE *a;<R>
  24.   JUNK root, *next, *p;<R>
  25.   BIG leftover;<R>
  26.   WORD junksize;<R>
  27.   root.next=NULL;<R>
  28.   next=&root;<R>
  29. <R>
  30.   while(1) {<R>
  31.     a=mustalloc(size);<R>
  32.     /* mustalloc==malloc,<R>
  33.     but aborts if failure;<R>
  34.     the function exits this<R>
  35.     way or via the break below<R>
  36.     at success.*/<R>
  37. <R>
  38.     if (<R>
  39.     (leftover=(PHYSICAL(a) & 0xffff)) +<R>
  40.     ((BIG) size)<R>
  41.     >>= Ox10000) {<R>
  42.       /* it's no good. */<R>
  43.       if (<R>
  44.       (junksize = 0x10000-leftover)<R>
  45.       << JUNKCHUNK)<R>
  46.       junksize=JUNKCHUNK;<R>
  47.       /* avoid endless<R>
  48.       teeny-tiny manipulations. */<R>
  49. <R>
  50.       free(a);<R>
  51.       next->>next=mustalloc(junksize);<R>
  52.       next=next->>next;<R>
  53.    }<R>
  54.    else<R>
  55.    break; /* success! */<R>
  56. }<R>
  57. /* free debris. */<R>
  58. next=root.next;<R>
  59. while(next)<R>
  60.   p=next;<R>
  61.   next=next->>next;<R>
  62.   free(p);<R>
  63.   }<R>
  64.   return a;<R>
  65. }<R>
  66. <R>
  67. /* End of File */ 
  68.  
  69.